﻿//
// GNS_NewPromoter.cpp
// MobileManageSys
//FuncTion  新增推广员/代理商
// Created by mr.l on 16/1/15.
//
//
#include "GnsClientFrame.h"
#include "GNS_NewPromoter.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "GNS_PlayerMgr.h"
#include "GNS_SceneMgr.h"
#include "GnsOften.h"

USING_NS_CC;
using namespace cocostudio::timeline;
using namespace ui;

#define winsize Director::getInstance()->getVisibleSize()
//extern ISocketMgr * _GameSock;
GNS_NewPromoter::GNS_NewPromoter()
{
    pGNS_NewPromoter=0;
	promoterZhangHao = nullptr;
	promoterRemark = nullptr;
	promoterFencheng = nullptr;
	promoterCode = nullptr;

    pPopWnd=nullptr;
    pTitle = nullptr;
    IDText=nullptr;
    RemText = nullptr;
    pCluesID="请输入[账号]有效长度";
    pCluesRem="请输入[推广员备注名]有效长度";
    pCluesInto="请输入[下级分成]有效长度";
    pCluesCode="请输入[注册码]有效长度";
    UserInfo=0;

	_promoterZhangHao = nullptr;
	_promoterRemark = nullptr;
	_promoterFencheng = nullptr;
	_promoterCode = nullptr;
}

GNS_NewPromoter::~GNS_NewPromoter()
{
    
}

GNS_NewPromoter* GNS_NewPromoter::createLayer()
{
    auto pGNS_NewPromoter = GNS_NewPromoter::create();
    auto pScene = Director::getInstance()->getRunningScene();
    if (!pScene)
        return 0;
    pScene->addChild(pGNS_NewPromoter);
    return pGNS_NewPromoter;
}

bool GNS_NewPromoter::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    
    return true;
}
void GNS_NewPromoter::SetNodeName(std::string CsbName)
{
    pGNS_NewPromoter = CSLoader::createNode(CsbName);
    pGNS_NewPromoter->setContentSize(winsize);
    ui::Helper::doLayout(pGNS_NewPromoter);
    addChild(pGNS_NewPromoter,11);
    GNS_PlayerMgr::getInstance()->_NodeChildVec.pushBack(pGNS_NewPromoter);
	GNS_NodeMgr::getInstance()->pushChildNode("GNS_NewPromoter", pGNS_NewPromoter);

    auto pBackBtn = static_cast<Button*>(pGNS_NewPromoter->getChildByName("NewPromoterBtn"));
    pBackBtn->setTag(1);
    pBackBtn->addTouchEventListener(CC_CALLBACK_2(GNS_NewPromoter::BtnCallBack, this));
    
    auto pSendDate = static_cast<Button *>(pGNS_NewPromoter->getChildByName("SendDateBtn"));
    pSendDate->setTag(2);
    pSendDate->addTouchEventListener(CC_CALLBACK_2(GNS_NewPromoter::BtnCallBack, this));
    
    UserInfo = GNS_PlayerMgr::getInstance()->pGetUserInfoResult._selfPercent;
    
    auto pSelfPercent = static_cast<Text*>(pGNS_NewPromoter->getChildByName("Text_8"));
    pSelfPercent->setString(StringUtils::format("%d",UserInfo));

    //PromoID = GNS_SceneMgr::getInstance()->MycreateEditBox("请输入推广员账号", Vec2(winsize.width*0.43, winsize.height*0.835), 24, Color3B::BLACK, 10, false);
	promoterZhangHao = static_cast<TextField*>(pGNS_NewPromoter->getChildByName("TextField_zhanghao"));
	promoterRemark = static_cast<TextField*>(pGNS_NewPromoter->getChildByName("TextField_remark"));

	_promoterZhangHao = GNS_Handler::createTMEditBoxWithExistFeild(promoterZhangHao, pGNS_NewPromoter, 10, EditBox::InputMode::NUMERIC, true);
	_promoterRemark = GNS_Handler::createTMEditBoxWithExistFeild(promoterRemark, pGNS_NewPromoter, 14);
	promoterZhangHao->setVisible(false);
	promoterRemark->setVisible(false);
    
    if(GNS_PlayerMgr::getInstance()->getUserLevel()==0)
    {
        auto TitleTsxt = static_cast<Text*>(pGNS_NewPromoter->getChildByName("Text_5"));
		TitleTsxt->setString(CCGnsOften::getUtf8_DirectAssignmentInProgram( "新增代理商"));
        auto NameText = static_cast<Text*>(pGNS_NewPromoter->getChildByName("Text_2"));
        NameText->setString(CCGnsOften::getUtf8_DirectAssignmentInProgram("代理商账号"));		
		promoterZhangHao->setPlaceHolder(CCGnsOften::getUtf8_DirectAssignmentInProgram("请输入代理商账号"));
		promoterRemark->setPlaceHolder(CCGnsOften::getUtf8_DirectAssignmentInProgram("无"));
		promoterRemark->setEnabled(false);

		_promoterRemark->setEnabled(false);
    }else{
		promoterZhangHao->setPlaceHolder(CCGnsOften::getUtf8_DirectAssignmentInProgram("请输入推广员账号"));
		promoterRemark->setEnabled(true);
		promoterRemark->setPlaceHolder(CCGnsOften::getUtf8_DirectAssignmentInProgram("请输入推广员备注名"));

		_promoterRemark->setEnabled(true);
		//PromoRem = GNS_SceneMgr::getInstance()->MycreateEditBox("请输入推广员备注名", Vec2(winsize.width*0.43, winsize.height*0.780), 24, Color3B::BLACK, 10, false);
        //pGNS_NewPromoter->addChild(PromoRem,12);

    }
    //PromoInto = GNS_SceneMgr::getInstance()->MycreateEditBox("下级分成值",Vec2(winsize.width*0.43, winsize.height*0.71), 24, Color3B::BLACK, 10, false);
    //pGNS_NewPromoter->addChild(PromoInto,12);
	promoterFencheng = static_cast<TextField*>(pGNS_NewPromoter->getChildByName("TextField_fencheng"));
	promoterFencheng->setPlaceHolder(CCGnsOften::getUtf8_DirectAssignmentInProgram("下级分成值"));

    
     //PromoCode = GNS_SceneMgr::getInstance()->MycreateEditBox("请输入注册码",Vec2(winsize.width*0.43, winsize.height*0.545), 24, Color3B::BLACK, 12, false);
    //pGNS_NewPromoter->addChild(PromoCode,12);
	promoterCode = static_cast<TextField*>(pGNS_NewPromoter->getChildByName("TextField_zhucema"));
	promoterCode->setPlaceHolder(CCGnsOften::getUtf8_DirectAssignmentInProgram("请输入注册码"));

	_promoterFencheng = GNS_Handler::createTMEditBoxWithExistFeild(promoterFencheng, pGNS_NewPromoter, 10, EditBox::InputMode::NUMERIC, true);
	_promoterCode = GNS_Handler::createTMEditBoxWithExistFeild(promoterCode, pGNS_NewPromoter, 10, EditBox::InputMode::NUMERIC, true);
}
void GNS_NewPromoter::ReceiveInformation()
{

}
void GNS_NewPromoter::BtnCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type){
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        int BtnTag = pButton->getTag();
        switch (BtnTag) {
            case 1:
            {
				Vector<Node *> pNode = GNS_PlayerMgr::getInstance()->_NodeChildVec;
                if (GNS_PlayerMgr::getInstance()->_NodeChildVec.size()!=0) {
                    GNS_PlayerMgr::getInstance()->_NodeChildVec.clear();
                    this->removeAllChildrenWithCleanup(true);
                    this->getParent()->removeChild(this);
                }
                //auto PromoterNode=GNS_PlayerMgr::getInstance()->_NodeHallVec.at(1);
                //PromoterNode->setVisible(true);
				//GNS_NodeMgr::getInstance()->eraseChildNode("GNS_NewPromoter");
				GNS_NodeMgr::getInstance()->setMenuVisible("Promoter", true);
                break;
            }
            case 2:
            {
                //发送信息
                CheckInput();
                
                break;
            }
            default:
                break;
        }
    }
}
void GNS_NewPromoter::CheckInput()
{
    std::string promoId =_promoterZhangHao->getText();
	std::string promoInto = _promoterFencheng->getText();
    std::string promoCode = _promoterCode->getText();
	std::string promoRem = "";
    if (GNS_PlayerMgr::getInstance()->getUserLevel()!=0) {
		promoRem = SystemMgr::getInstance()->utf8ToGb(_promoterRemark->getText());
        if (!CCGnsOften::checkRemarkName(promoRem))
        {
            OnUIAction(pCluesRem, true);
            return;
        }
    }
    if (!CCGnsOften::checkMgrID(promoId)) {
        OnUIAction(pCluesID,true);
        return;
    }else if (promoCode.length()<=0||promoCode.length()>13)
    {
        OnUIAction(pCluesCode, true);
        return;
    }else if (!CCGnsOften::isNumber(promoInto))
    {
        OnUIAction(pCluesInto, true);
        return;
    }else{
        CMD_CS_AgentUserRegister pAgUserReg;
        pAgUserReg.iVersion=GNS_PlayerMgr::getInstance()->getCurrentVersionClient();
#if (CC_TARGET_PLATFORM==CC_PLATFORM_ANDROID)
		pAgUserReg.iPlatformNo = 100;
#elif (CC_TARGET_PLATFORM==CC_PLATFORM_IOS)
		pAgUserReg.iAppId = 104;
#else
		pAgUserReg.iAppId = 105;
#endif
        std::string md5 = "1111111111111111111111";
		memcpy(pAgUserReg.szMachineCode,md5.c_str(),MACHINE_CODE_LEN);
        pAgUserReg.iTrenchId=1;
		memcpy(pAgUserReg.szName,promoId.c_str(),USER_NAME_LEN);
        if (GNS_PlayerMgr::getInstance()->getUserLevel()!=0) {
			memcpy(pAgUserReg.szRemark,promoRem.c_str(),USER_NAME_LEN);
        }
        memcpy(pAgUserReg.szRegisterCode, promoCode.c_str(), sizeof(pAgUserReg.szRegisterCode));
        pAgUserReg.iLowerLevelPercent=atoi(promoInto.c_str());
		char szPass[USER_PASSWORD_LEN] = {"123456"};
		memcpy(pAgUserReg.szPwd, SystemMgr::getInstance()->md5String(szPass), USER_PASSWORD_LEN);
        pAgUserReg.iSelfPercent=UserInfo-atoi(promoInto.c_str());
		memcpy(pAgUserReg.szAgentName, StringUtils::format("%lld", GNS_PlayerMgr::getInstance()->getMyUseridx()).c_str(), USER_NAME_LEN);
        GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_USER_REGISTER, (char*)&pAgUserReg, sizeof(pAgUserReg));
    }
}
PopWndLayer * GNS_NewPromoter::OnUIAction(std::string Text,bool isUTF)
{
    pPopWnd = PopWndLayer::createScene("Popwnd.png", "PageClose.png");
    pPopWnd->setClickWndOutsideClose(true);
    pPopWnd->SetTitleWithUTF("提示!");
	if (isUTF)
		pPopWnd->SetContentTextWithUTF(Text, Color3B::BLACK);
	else
		pPopWnd->SetContentTextWithGB(Text, Color3B::BLACK);
    return pPopWnd;
}